#include<bits/stdc++.h>
using namespace std;
struct student
{
	int score;
	string name;
	bool operator < (const student &x) const
	{
		if(x.score==score)
			return name<x.name;
		return score>x.score;
	}
}s[100];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>s[i].name>>s[i].score;
    sort(s+1,s+n+1);
    for(int i=1;i<n;i++)
    	cout<<s[i].name<<" "<<s[i].score<<endl;
	cout<<s[n].name<<" "<<s[n].score;
	return 0;
}
